# -*- cperl -*-
use strict;
use Benchmark;

# Similar to comment and mkattr
# Comments are handled in a specific way, and string attributes must be
# enclosed in double quotes.
# This leaves some cases to test.

use vars qw($ctx $tvob $tele);
use constant MSWIN	=> $^O =~ /MSWin|Windows_NT/i ? 1 : 0;
use constant CYGWIN	=> $^O =~ /cygwin/i ? 1 : 0;
use Test::More tests => 16;
select(STDERR); $| = 1; select(STDOUT); $| = 1;
use ClearCase::Argv;
use Argv 1.28; # for summary to work on perl 5.14+

if (!defined do 'test.cfg') {
  if ($@) {
    die "$@\n";
  }
  warn "Failed to open 'test.cfg', using default values\n" if $!;
}
ClearCase::Argv->summary;      # start keeping stats
ClearCase::Argv->ipc(1);
my $ct = ClearCase::Argv->new({autochomp=>1});
die "This test needs to be run in a view, in a vob\n"
  unless $ct->ls([qw(-s -d)], '.')->stderr(0)->qx;
my $silent = $ct->clone({stdout=>0, stderr=>0});
$silent->dbglevel(1) if @ARGV;
$tvob = $ct->des(['-s'], 'vob:.')->qx unless $tvob;
$tele = '.' unless $tele;
my $wrap = $silent->clone({ct=>$ctx}) if $ctx;

sub one_block {
  my ($value, $mode) = @_;
  (my $vrep = $value) =~ s/\n/<newline>/g;
  ok(!$silent->des(['-fmt', $value], '.')->system,
     qq(cleartool: "$vrep" ($mode mode, array syntax)));
  ok(!$silent->argv(qq(des -fmt "$value" .))->system,
     qq(cleartool: "$vrep" ($mode mode, string syntax)));
 SKIP: {
    skip "No wrapper no fun", 2 unless $wrap;
    ok(!$wrap->des(['-fmt', $value], '.')->system,
       qq(wrapper: "$vrep" ($mode mode, array syntax)));
    ok(!$wrap->argv(qq(des -fmt "$value" .))->system,
       qq(wrapper: "$vrep" ($mode mode, string syntax)));
  }
}
sub one_pass {
  my $ipc = shift;
  my $mode = $ipc? 'ipc' : 'fork';
  $silent->ipc($ipc);
  $wrap->ipc($ipc) if $wrap;
  one_block(q(%d\n), $mode);
  one_block(q('%Fu' %d\n), $mode);
}
one_pass(0);
one_pass(1);

print STDERR "\n", ClearCase::Argv->summary;   # print out the stats we kept
